package com.ihighest.app.infox.activity;

import org.json.JSONException;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ListView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.core.Application;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.PlantLeftListViewAdapter;
import com.ihighest.app.infox.fragment.PlantFragment;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 种植百科界面
 * 
 * @author yyh Created at 2015-10-13 下午4:21:05
 * @version 1.0
 */
public class PlantEncyclopediaActivity extends FragmentActivity {

	ActivityTitle title;
	ListView plantListView;
	PlantFragment fragment;
	PlantLeftListViewAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Application.getInstance().addActivity(this);
		setContentView(R.layout.infox_activity_plantencyclopedia);

		title = (ActivityTitle) findViewById(R.id.infox_plant_title);
		title.setViewVersible(title.rightLayout, title.titleBack);
		title.setViewGone(title.titleSetting, title.titleMsg, title.titleRefresh, title.titleOther);
		title.setTitle(this.getString(R.string.findActivity_plant));
		initUI();
	}

	private void initUI() {
		plantListView = (ListView) findViewById(R.id.id_plant_leftlistview);
		Api.data("wiki_class_list.json", new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				adapter = new PlantLeftListViewAdapter(mContext, result.getDatas());
				plantListView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				if (result.getDatas() != null && result.getDatas().length() > 0) {
					plantListView.setSelection(0);
					try {
						fragment = new PlantFragment(mContext, result.getDatas().getJSONObject(0).getJSONArray("sub"));
						getSupportFragmentManager().beginTransaction().add(R.id.id_plant_framelayout, fragment).commit();
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public void handleFailure(Exception e, String msg) {
			}
		}, this);
	}

}
